package com.example.neochevbook;


import java.util.ArrayList;
import java.util.Date;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Modele unmodele;
	private Button Connexion = null;
	private Button Sinscrire = null;
	private TextView MDPoublie = null;
	private EditText Identifiant = null;
	private EditText MDP = null;
	private ArrayList<Utilisateur> UnUtilisateur = new ArrayList<Utilisateur>();
	private int i = 0;
	private String unindiv;
	private String monid;
	private String monMDDDDDDP;
	private int monidentifiant;
	private String monMDP;
	private Button test;
	private Connexion maconnexion;
	private AsyncTask<String, String, Boolean> mthreadcons = null; 
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		

		Connexion = (Button) findViewById(R.id.buttonConnexion);
		Sinscrire = (Button) findViewById(R.id.buttonSinscrire);
		MDPoublie = (TextView) findViewById(R.id.textViewMDPoublie);
			
		Connexion.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Identifiant = (EditText) findViewById(R.id.editTextIdentifiant);
				MDP = (EditText) findViewById(R.id.editTextMDP); 
				
				 //UnUtilisateur = unmodele.listUtilisateur();
				monid = Identifiant.getText().toString(); 
				monMDDDDDDP = MDP.getText().toString(); 
			
				monidentifiant = unmodele.trouveClient(Integer.parseInt(monid)).getId_utilisateur();
				monMDP = unmodele.trouveClient(Integer.parseInt(monid)).getMdp();

				
				Toast.makeText(getApplicationContext(), monidentifiant, Toast.LENGTH_LONG).show();
				

				if(monidentifiant == Integer.parseInt(monid) && monMDP == monMDDDDDDP )
				{

						//Toast.makeText(getApplicationContext(), monidentifiant, Toast.LENGTH_LONG).show();
						

				Intent myIntent = new Intent(getApplicationContext(), AccesAppartement.class);
				startActivity(myIntent);
				}
				else
				{
					Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
					startActivity(myIntent);
				}
		}
		});
		
		Sinscrire.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(getApplicationContext(), Inscription.class);
				startActivity(myIntent);
			}
		}); 
		
		MDPoublie.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent myIntent = new Intent(getApplicationContext(), MailDenvoieMotDePasse.class);
				startActivity(myIntent);
			}
		});
		
		test = (Button) findViewById(R.id.buttonTest);
		test.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				String[] mesparams = {"", "", "http://goshka.fr/ppe4/connexion/connect.php"};
				mthreadcons = new Connexion(MainActivity.this).execute(mesparams);
				
			}
		}); 
	
		unmodele = new Modele();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	public void alertmsg(String title, String msg) {
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(title);
		adb.setPositiveButton("Ok", null);
		adb.setMessage(msg);
		adb.show();
		}
	
	public void apresconnexion(StringBuilder sb) {
		
		Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG).show();

		JsonElement json = new JsonParser().parse(sb.toString());
		Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_LONG).show();

		JsonArray varray = json.getAsJsonArray();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-mm-dd").create();
		ArrayList<Utilisateur> vlist=new ArrayList<Utilisateur>();
		 for(JsonElement obj : varray )
		    {
			 
			 Utilisateur vutilisateur = gson.fromJson(obj.getAsJsonObject(),Utilisateur.class);
			 //gson.fromJson(obj.getAsJsonObject(),Utilisateur.class);
			 Toast.makeText(getApplicationContext(), vutilisateur.getNom(), Toast.LENGTH_LONG).show();
			 vutilisateur.setCp(0);
			 vutilisateur.setId_utilisateur(0);
			 vutilisateur.setTelephone(0);
			 vlist.add(vutilisateur);
			 
		    }
		Modele vmod=new Modele();
		//vmod.delclient();
		vmod.addclient(vlist);
		Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG).show();

		
	}

}
